package com.shuoyue.ycgk.ui.mycourse;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.course.FragmentCourseTab;
import com.shuoyue.ycgk.ui.mine.learnprogress.CalenderAdapter;
import com.shuoyue.ycgk.ui.mine.learnprogress.DayInfo;
import com.shuoyue.ycgk.ui.mine.learnprogress.WeekAdapter;
import com.shuoyue.ycgk.ui.mine.learnprogress.contract.DealClassProgressContract;
import com.shuoyue.ycgk.utils.GridSpacingItemDecoration;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.views.viewpager_scroll.SuperViewPager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentMyCourse extends BaseMvpFragment {


    @BindView(R.id.last_mouth)
    ImageView lastMouth;
    @BindView(R.id.mouth)
    TextView mouth;
    @BindView(R.id.next_mouth)
    ImageView nextMouth;
    @BindView(R.id.week)
    RecyclerView weekRecycleView;
    @BindView(R.id.dday)
    RecyclerView dayRecycleView;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.info)
    TextView info;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.learnNum)
    TextView learnNum;
    @BindView(R.id.subNum)
    TextView subNum;
    @BindView(R.id.lay_info)
    LinearLayout layInfo;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.lay_bottom)
    LinearLayout layBottom;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    SuperViewPager viewPager;


    CalenderAdapter calenderAdapter;
    Calendar calendar = Calendar.getInstance();


    boolean isAskingNetwork = false;
    public IndexFragmentStateAdapter indexFragmentStateAdapter;
    List<BaseMvpFragment> fragmentMainItems = new ArrayList<>();

    @Override
    public void initParams(Bundle bundle) {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_course;
    }

    @Override
    protected void initView() {
        weekRecycleView.setLayoutManager(new GridLayoutManager(mContext, 7));
        weekRecycleView.setAdapter(new WeekAdapter());
        dayRecycleView.setLayoutManager(new GridLayoutManager(mContext, 7));
        dayRecycleView.addItemDecoration(new GridSpacingItemDecoration(7,
                SizeUtil.dip2px(mContext, 8), false));
        dayRecycleView.setAdapter(new WeekAdapter());
        calenderAdapter = new CalenderAdapter(null);
        calenderAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (!isAskingNetwork) {
                DayInfo dayInfo = calenderAdapter.getItem(position);
                for (DayInfo dayInfoAll : calenderAdapter.getData()) {
                    dayInfoAll.setSelect(false);
                }
                dayInfo.setSelect(true);
                calenderAdapter.notifyDataSetChanged();
                Calendar tagerCal = Calendar.getInstance();
                tagerCal.setTime(dayInfo.getData());
            }
        });
        dayRecycleView.setAdapter(calenderAdapter);


        fragmentMainItems.add(FramentMyCourseAll.getInstance());
        fragmentMainItems.add(FragmentMyCoursePauches.getInstance());
        fragmentMainItems.add(FragmentMyCourseFree.getInstance());
        indexFragmentStateAdapter = new IndexFragmentStateAdapter(getChildFragmentManager(), fragmentMainItems);
        viewPager.setAdapter(indexFragmentStateAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }


    @Override
    protected void initData() {
        mouth.setText(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1));

        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK); //星期天开始1-7
        calendar.add(Calendar.DAY_OF_MONTH, -day_of_week + 1);//第一个
        calenderAdapter.resetData(getDayInfos(calendar.getTime()));

    }


    /**
     * @return
     */
    private List<DayInfo> getDayInfos(Date stateDate) {
        Calendar calendarToday = Calendar.getInstance(Constant.timeZone);
        int nowyear = calendarToday.get(Calendar.YEAR);
        int nowmonth = calendarToday.get(Calendar.MONTH);
        int nowdayOmonth = calendarToday.get(Calendar.DAY_OF_MONTH);
        Calendar calendarNow = Calendar.getInstance(Constant.timeZone);
        calendarNow.setTime(stateDate);
        List<DayInfo> dayInfos = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            DayInfo dayInfo = new DayInfo();
            dayInfo.setData(calendarNow.getTime());
            dayInfo.setDayInMouth(calendarNow.get(Calendar.DAY_OF_MONTH));
            dayInfos.add(dayInfo);
            if (nowyear == calendarNow.get(Calendar.YEAR)
                    && nowmonth == calendarNow.get(Calendar.MONTH)
                    && nowdayOmonth == calendarNow.get(Calendar.DAY_OF_MONTH)) {
                dayInfo.setSelect(true);
            }
            calendarNow.add(Calendar.DAY_OF_MONTH, 1);

        }
        return dayInfos;
    }

    @OnClick({R.id.last_mouth, R.id.next_mouth})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.last_mouth:
                calendar.add(Calendar.WEEK_OF_MONTH, -1);
                calenderAdapter.resetData(getDayInfos(calendar.getTime()));
                mouth.setText(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1));
                break;
            case R.id.next_mouth:
                calendar.add(Calendar.WEEK_OF_MONTH, 1);
                calenderAdapter.resetData(getDayInfos(calendar.getTime()));
                mouth.setText(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1));
                break;
        }
    }

    public static FragmentMyCourse getInstance() {
        FragmentMyCourse fragment = new FragmentMyCourse();
        return fragment;
    }


    @Override
    public String getTitle() {
        return "我的课程";
    }
}
